<script setup lang="ts">
import { ref, computed, onBeforeMount } from 'vue'
import { api } from '@/api/api'
import { useUserStore } from '@/stores/user'
import { ElLoading, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import type { BriefParameter } from '@/types/ParameterType'
import { useParameterEditSId } from '@/stores/parameterEditSId'

const userStore = useUserStore()
const standardId = useParameterEditSId()
const router = useRouter()

function toInfo(_id: number, sId: number) {
  standardId.id = sId
  router.push('/home/infoT/' + _id.toString())
}

var tableData = ref<BriefParameter[]>([])
const allTableData = ref<BriefParameter[]>([])

onBeforeMount(() => {
  if (userStore.userInfo.id == undefined) {
    ElMessageBox.alert('用户未登录或登陆超时！', '错误', {
      cancelButtonText: '确认',
      callback: () => {
        router.replace({ path: '/' })
      }
    })
  }
  let loading = ElLoading.service()
  api
    .getCompletedBriefParameterByEmpId(userStore.userInfo.id ?? 0)
    .then((resp) => {
      allTableData.value = resp
      count = allTableData.value.length
      loading.close()
    })
    .catch(() => {
      loading.close()
      ElMessageBox.alert('读取数据失败', '错误', {
        confirmButtonText: '确认'
      })
    })
  tableData = computed(() => {
    let result = allTableData.value.slice(
      (currentPage.value - 1) * currentSize.value,
      currentPage.value * currentSize.value
    )
    return result
  })
})
const currentPage = ref(1)
const currentSize = ref(10)
var count: number
count = allTableData.value.length

const currentChange = (page: number) => {
  currentPage.value = page
}

const sizeChange = (size: number) => {
  currentSize.value = size
}
</script>

<template>
  <div class="main">
    <h1 class="h1">已完成任务</h1>
    <div class="listbox">
      <div class="number">
        <h3>共计{{ count }}条项目</h3>
      </div>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column type="index" />
        <el-table-column prop="id" label="ID" />
        <el-table-column label="标准" prop="standardName"></el-table-column>
        <el-table-column label="大类" prop="category"></el-table-column>
        <el-table-column label="类别" prop="type"></el-table-column>
        <el-table-column prop="name" label="名称" width="300" />
        <el-table-column prop="creator" label="发起人" width="150" />
        <el-table-column label=" " width="100">
          <template #default="scope">
            <el-button
              link
              type="primary"
              size="small"
              @click="toInfo(scope.row.id, scope.row.standardId)"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <div class="pager">
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      :current-page="currentPage"
      :page-size="currentSize"
      :background="true"
      layout="prev, pager, next, jumper"
      :total="count"
      :hide-on-single-page="true"
    />
  </div>
</template>

<style scoped>
.main {
  width: 100%;

  .h1 {
    margin-left: 50px;
    margin-top: 50px;
  }
}

.listbox {
  margin-top: 100px;
  margin-left: 100px;
  margin-right: 100px;
  padding-left: 20px;
  padding-right: 20px;
}

.pager {
  margin-left: 100px;
  margin-top: 50px;
}
</style>
